﻿@model GiftCardModel
@using Telerik.Web.Mvc.UI;
@using SmartStore.Core.Domain.Catalog;

@Html.ValidationSummary(false)

@Html.HiddenFor(model => model.Id)

@Html.SmartStore().TabStrip().Style(TabsStyle.Material).Name("giftcard-edit").Items(x =>
{
    x.Add().Text(T("Admin.GiftCards.Info").Text).Content(TabInfo()).Selected(true);
    if (Model.Id > 0)
    {
        x.Add().Text(T("Admin.GiftCards.History").Text).Content(TabHistory());
    }
    	
    //generate an event
    EngineContext.Current.Resolve<IEventPublisher>().Publish(new TabStripCreated(x, "giftcard-edit", this.Html, this.Model));
})

@helper TabInfo()
{
	<script type="text/javascript">
		$(document).ready(function () {

			$("#@Html.FieldIdFor(model => model.GiftCardTypeId)").change(function () {
				if ($(this).val() == '@((int)GiftCardType.Virtual)') {
					$('#pnlRecipientEmail').show();
					$('#pnlSenderEmail').show();
					$('#pnlNotifyRecipient').show();
				}
				else {
					$('#pnlRecipientEmail').hide();
					$('#pnlSenderEmail').hide();
					$('#pnlNotifyRecipient').hide();
				}
			}).trigger('change');

            $('#generateCouponCode').click(function () {
                $.ajax({
                    cache:false,
                    type: "POST",
                    url: "@(Url.Action("GenerateCouponCode", "GiftCard"))",
                    success: function (data) {
                         $('#@Html.FieldIdFor(model => model.GiftCardCouponCode)').val(data.CouponCode);
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('Failed to generate code.')
                    }  
                });
            });

		});
    </script>
	
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.GiftCardTypeId)
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.GiftCardTypeId, ((GiftCardType)Model.GiftCardTypeId).ToSelectList())
                @Html.ValidationMessageFor(model => model.GiftCardTypeId)
            </td>
        </tr>
        @if (Model.PurchasedWithOrderId.HasValue)
        {
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.PurchasedWithOrderId)
                </td>
                <td class="adminData">
					<div class="form-control-plaintext">
						<a href="@Url.Action("Edit", "Order", new { Id = Model.PurchasedWithOrderId.Value })">@Model.PurchasedWithOrderId.Value</a>
					</div>
                </td>
            </tr>
        }
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Amount)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Amount, new { postfix = Model.PrimaryStoreCurrencyCode }) 
                @Html.ValidationMessageFor(model => model.Amount)
            </td>
        </tr>
        @if (Model.Id > 0)
        {
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.RemainingAmountStr)
                </td>
                <td class="adminData">
					@Html.TextBoxFor(model => model.RemainingAmountStr, new { @readonly = "readonly", @class = "form-control-plaintext" })
                </td>
            </tr>
        }
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.IsGiftCardActivated)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.IsGiftCardActivated)
                @Html.ValidationMessageFor(model => model.IsGiftCardActivated)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.GiftCardCouponCode)
            </td>
            <td class="adminData">

				<div class="input-group">
					@Html.EditorFor(model => model.GiftCardCouponCode)

					<div class="input-group-append">
						<button type="button" name="generateCouponCode" id="generateCouponCode" class="btn btn-outline-secondary">
							<i class="fa fa-cog"></i>
							<span>@T("Admin.GiftCards.Fields.GiftCardCouponCode.Generate")</span>
						</button>
					</div>
				</div>

                @Html.ValidationMessageFor(model => model.GiftCardCouponCode)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.RecipientName)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.RecipientName)
                @Html.ValidationMessageFor(model => model.RecipientName)
            </td>
        </tr>
        <tr id="pnlRecipientEmail">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.RecipientEmail)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.RecipientEmail)
                @Html.ValidationMessageFor(model => model.RecipientEmail)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.SenderName)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SenderName)
                @Html.ValidationMessageFor(model => model.SenderName)
            </td>
        </tr>
        <tr id="pnlSenderEmail">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.SenderEmail)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SenderEmail)
                @Html.ValidationMessageFor(model => model.SenderEmail)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Message)
            </td>
            <td class="adminData">
                @Html.TextAreaFor(model => model.Message)
                @Html.ValidationMessageFor(model => model.Message)
            </td>
        </tr>
        @if (Model.Id > 0)
        {
            <tr id="pnlNotifyRecipient">
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.IsRecipientNotified)
                </td>
                <td class="adminData">
					@T(Model.IsRecipientNotified ? "Admin.Common.Yes" : "Admin.Common.No")
					&nbsp;
                    <button type="submit" name="notifyRecipient" value="notifyRecipient" class="btn btn-secondary">
						<i class="fa fa-envelope"></i>
						<span>@T("Admin.GiftCards.Fields.IsRecipientNotified.Notify")</span>
					</button>
                </td>
            </tr>
        }
        @if (Model.Id > 0)
		{
			<tr>
				<td class="adminTitle">
					@Html.SmartLabelFor(model => model.CreatedOn)
				</td>
				<td class="adminData">
					<div class="form-control-plaintext">
						@Html.DisplayFor(model => model.CreatedOn)
					</div>
				</td>
			</tr>
		}
    </table>
}

@helper TabHistory()
{
    var gridPageSize = EngineContext.Current.Resolve<SmartStore.Core.Domain.Common.AdminAreaSettings>().GridPageSize;

    @(Html.Telerik().Grid<GiftCardModel.GiftCardUsageHistoryModel>()
        .Name("usagehistory-grid")
        .DataKeys(x =>
        {
            x.Add(y => y.Id).RouteKey("Id");
        })
        .Columns(columns =>
        {
            columns.Bound(x => x.UsedValue).Width(200);
            columns.Bound(x => x.OrderId)
                .Width(100)
                .Template(x => Html.ActionLink(T("Admin.Common.View").Text, "Edit", "Order", new { id = x.OrderId }, new { }))
                .ClientTemplate("<a href=\"" + @Url.Content("~/Admin/Order/Edit/") + "<#= OrderId #>\">" + T("Admin.Common.View").Text + "</a>");
            columns.Bound(x => x.CreatedOn).Width(200);

        })
        .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
        .DataBinding(dataBinding =>
        {
            dataBinding.Ajax().Select("UsageHistoryList", "GiftCard", new { giftCardId = Model.Id });
        })
		.PreserveGridState()
        .EnableCustomBinding(true))
    
}